﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedBlueBalls
{
    class RedDate
    {
        private static bool analyzed = false;
        private static bool checkBest = false;
        private static bool last1;
        private static bool last2;
        private static bool last3;
        private static bool last4;
        private static int day;

        public static void analyze(RedBlueBallRecord[] records)
        {
            if (checkBest)
            {
                int count = 0;
                foreach (RedBlueBallRecord record in records)
                {
                    if (record.HasRedBall(record.GetDay()))
                        count++;
                }

                Tracer.AppendMessage(String.Format("Same Day Red {0}: {1}", count, records.Length)); // 1/5
            }
            int index = records.Length - 1;
            last1 = records[index].HasRedBall(records[index].GetDay());
            last2 = records[index - 1].HasRedBall(records[index - 1].GetDay());
            last3 = records[index - 2].HasRedBall(records[index - 2].GetDay());
            last4 = records[index - 3].HasRedBall(records[index - 3].GetDay());
            day = records[index].GetNextDate().Day;

            analyzed = true;
        }

        public static bool predict(RedBlueBallRecord candidate)
        {
            if (!analyzed)
            {
                Tracer.AppendMessage("Please analyze the data first!");
                return false;
            }

            if (last1 || last2 || last3 || last4)
            {
                // Should not cantain the day
                if (candidate.HasRedBall(day))
                    return false;
            }
            else
            {
                // Should contain the day
                if (!candidate.HasRedBall(day))
                    return false;
            }   
           
            return true;
        }
    }
}
